/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Role;
import br.com.jr2tecnologia.model.repository.LoginRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class LoginDAO extends GenericDAO<Login, LoginRepository> implements LoginRepository {
    
    private static final Logger logger = LoggerFactory.getLogger();

    public Login findById(Long id) {
        return (Login)em.createNamedQuery("Login.findByPkLogin").
                setParameter("pkLogin", id).getSingleResult();
        
    }

    public Login findByName(String name) {
        try{
            return (Login)em.createNamedQuery("Login.findByName").
                setParameter("name", name).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public Login findByEmail(String email) {
        try{
            return (Login)em.createNamedQuery("Login.findByEmail").
                setParameter("email", email).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.SEVERE, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.SEVERE, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<Login> list(Login login){
        try{
            Company company = login.getCompany();
            Role role = login.getRole();
            BankAccount bankAccount = login.getBankAccount();
            String hql = "select login from Login as login ";
            hql+=" left outer join login.company as company";
            hql+=" left outer join login.role as role";
            hql+=" left outer join login.bankAccount as bankAccount";
            hql+=" where login.pkLogin = login.pkLogin";

            if(login.getName() != null && !login.getName().equals(""))
                hql+=" and login.name like :login_name";
            if(login.getEmail() != null && !login.getEmail().equals(""))
                hql+=" and login.email like :login_email";
            if(company != null && company.getPkCompany() != null)
                hql+=" and company.pkCompany = :pkCompany";
            if(role != null && role.getPkRole() != null)
                hql+=" and role.pkRole = :pkRole";
            if(bankAccount != null && bankAccount.getPkBankAccount() != null)
                hql+=" and bankAccount.pkBankAccount = :pkBankAccount";
            hql+=" order by company.name, login.email, role.name";

            Query query = em.createQuery(hql);
            if(login.getName() != null && !login.getName().equals(""))
                query.setParameter("login_name", login.getName()+"%");
            if(login.getEmail() != null && !login.getEmail().equals(""))
                query.setParameter("login_email", login.getEmail()+"%");
            if(company != null && company.getPkCompany() != null)
                query.setParameter("pkCompany", company.getPkCompany());
            if(role != null && role.getPkRole() != null)
                query.setParameter("pkRole", role.getPkRole());
            if(bankAccount != null && bankAccount.getPkBankAccount() != null)
                query.setParameter("pkBankAccount", bankAccount.getPkBankAccount());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list login",e);
            throw new DAOException(e);
        }
    }

    public List<Login> listAll(){
        try{
            return em.createNamedQuery("Login.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

}
